package kr.pe.johnkim.checkmileage.mobile.rest.controller;

import kr.pe.johnkim.checkmileage.mobile.domain.CheckMileageMerchantMileage;
import kr.pe.johnkim.checkmileage.mobile.rest.service.CheckMileageMerchantMileageService;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * Check Mileage 가맹점 등록 마일리지 관련 Controller 클래스 이다.
 * 
 * @author : johnkim <cale9797@gmail.com / Bettersoft, Inc.>
 * @Date : 12. 7. 24
 */

@Controller
@Path("/checkMileageMerchantMileageController")
public class CheckMileageMerchantMileageController {

    // SLF4J Logging
	private Logger logger = LoggerFactory.getLogger(CheckMileageMerchantMileageController.class);

    @Autowired
    private CheckMileageMerchantMileageService checkMileageMerchantMileageService = null;

    /**
     * 메뉴/서비스 마일리지를 등록 한다.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/registerMerchantMileage")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public CheckMileageMerchantMileage registerMerchantMileage(InputStream is) throws Exception {
        logger.info("registerMerchantMileage");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMerchantMileage = new JSONObject(line);
        JSONObject jsonMerchantMileage2 = jsonMerchantMileage.getJSONObject("checkMileageMerchantMileage");

        CheckMileageMerchantMileage parameterMerchantMileage = new CheckMileageMerchantMileage();

        parameterMerchantMileage.setContent(jsonMerchantMileage2.getString("content"));
        parameterMerchantMileage.setMileage(jsonMerchantMileage2.getString("mileage"));
        parameterMerchantMileage.setActivateYn(jsonMerchantMileage2.getString("activateYn"));
        parameterMerchantMileage.setModifyDate(jsonMerchantMileage2.getString("modifyDate"));
        parameterMerchantMileage.setRegisterDate(jsonMerchantMileage2.getString("registerDate"));
        parameterMerchantMileage.setCheckMileageMerchantsMerchantId(jsonMerchantMileage2.getString("checkMileageMerchantsMerchantId"));

        CheckMileageMerchantMileage resultMerchantMileage = new CheckMileageMerchantMileage();

        try {
            checkMileageMerchantMileageService.registerMerchantMileage(parameterMerchantMileage);
            resultMerchantMileage.setResult("SUCCESS");
        } catch (Exception e) {
            resultMerchantMileage.setResult("FAIL");
            logger.error("Fail to register merchant mileage.");
        }

        return resultMerchantMileage;
    }

    /**
     * 메뉴/서비스 마일리지 목록을 가져 온다.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/selectMerchantMileageList")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public List<CheckMileageMerchantMileage> selectMerchantMileageList(InputStream is) throws Exception {
        logger.info("selectMerchantMileageList");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMerchantMileage = new JSONObject(line);
        JSONObject jsonMerchantMileage2 = jsonMerchantMileage.getJSONObject("checkMileageMerchantMileage");

        CheckMileageMerchantMileage parameterMerchantMileage = new CheckMileageMerchantMileage();

        parameterMerchantMileage.setActivateYn(jsonMerchantMileage2.getString("activateYn"));
        parameterMerchantMileage.setCheckMileageMerchantsMerchantId(jsonMerchantMileage2.getString("checkMileageMerchantsMerchantId"));

        return checkMileageMerchantMileageService.selectMerchantMileageList(parameterMerchantMileage);
    }

    /**
     * 메뉴/서비스 마일리지를 수정 한다.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/updateMerchantMileageInformation")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public CheckMileageMerchantMileage updateMerchantMileageInformation(InputStream is) throws Exception {
        logger.info("updateMerchantMileageInformation");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMerchantMileage = new JSONObject(line);
        JSONObject jsonMerchantMileage2 = jsonMerchantMileage.getJSONObject("checkMileageMerchantMileage");

        CheckMileageMerchantMileage parameterMerchantMileage = new CheckMileageMerchantMileage();

        parameterMerchantMileage.setIdCheckMileageMerchantMileages(jsonMerchantMileage2.getString("idCheckMileageMerchantMileages"));
        parameterMerchantMileage.setContent(jsonMerchantMileage2.getString("content"));
        parameterMerchantMileage.setMileage(jsonMerchantMileage2.getString("mileage"));
        parameterMerchantMileage.setActivateYn(jsonMerchantMileage2.getString("activateYn"));
        parameterMerchantMileage.setModifyDate(jsonMerchantMileage2.getString("modifyDate"));

        CheckMileageMerchantMileage resultMerchantMileage = new CheckMileageMerchantMileage();

        try {
            checkMileageMerchantMileageService.updateMerchantMileageInformation(parameterMerchantMileage);
            resultMerchantMileage.setResult("SUCCESS");
        } catch (Exception e) {
            resultMerchantMileage.setResult("FAIL");
            logger.error("Fail to update merchant mileage information.");
        }

        return resultMerchantMileage;
    }

    /**
     * 메뉴/서비스 마일리지를 비활성화 한다.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/updateDeactivateMerchantMileage")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public CheckMileageMerchantMileage updateDeactivateMerchantMileage(InputStream is) throws Exception {
        logger.info("updateMerchantMileageInformation");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMerchantMileage = new JSONObject(line);
        JSONObject jsonMerchantMileage2 = jsonMerchantMileage.getJSONObject("checkMileageMerchantMileage");

        CheckMileageMerchantMileage parameterMerchantMileage = new CheckMileageMerchantMileage();

        parameterMerchantMileage.setIdCheckMileageMerchantMileages(jsonMerchantMileage2.getString("idCheckMileageMerchantMileages"));
        parameterMerchantMileage.setActivateYn(jsonMerchantMileage2.getString("activateYn"));
        parameterMerchantMileage.setModifyDate(jsonMerchantMileage2.getString("modifyDate"));

        CheckMileageMerchantMileage resultMerchantMileage = new CheckMileageMerchantMileage();

        try {
            checkMileageMerchantMileageService.updateDeactivateMerchantMileage(parameterMerchantMileage);
            resultMerchantMileage.setResult("SUCCESS");
        } catch (Exception e) {
            resultMerchantMileage.setResult("FAIL");
            logger.error("Fail to update deactivate merchant mileage.");
        }

        return resultMerchantMileage;
    }
}
